package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import View.ContactDeleterView;
import View.MainFrame;

/**
 * Action class for delete several contact functionality
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class ContactDeleterAction implements ActionListener {
	ContactController ctrl;
	ContactDeleterView view;
	
	/**
	 * default construction 
	 * @param ctrl the main controller of the application
	 * @param view the dialog displayed to delete contacts
	 */
	public ContactDeleterAction(ContactController ctrl, ContactDeleterView view) {
		this.ctrl = ctrl;
		this.view = view;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		JButton obj = (JButton) e.getSource();
		
		if(obj.getText().equals("Ok")){
			int[] toDelete = view.getSelected();
			for(int i=toDelete.length-1;i>=0;i--){
				//we begin by the end because it won't affect the other ids
				//in fact if we begin by the 0 and delete the 0 the id 1 wil become 0 and so on
				//so the selected id won't matches the ones selected by the user
				ctrl.removeContact(toDelete[i]);
			}
			view.dispose();
			view.setVisible(false);
		} else if(obj.getText().equals("Cancel")){
			view.dispose();
			//view.setVisible(false);
		}
	}

}
